package com.ddy.test.creditcardmanage.dao

import androidx.lifecycle.LiveData
import androidx.room.*
import com.ddy.test.creditcardmanage.model.Card
import com.ddy.test.creditcardmanage.model.CardRecord

@Dao
interface CardDao{

    @Query("SELECT * FROM credit_card_manage order by statementDate DESC")
    fun getAll(): LiveData<List<Card>>

    @Query("SELECT * FROM credit_card_manage WHERE id = :id")
    fun getLiveCardById(id: Long): LiveData<Card>

    @Query("SELECT * FROM credit_card_manage WHERE id = :id")
    fun getCardById(id: Long): Card

    @Insert
    fun addCard(card: Card)

    @Query("DELETE FROM credit_card_manage WHERE id=:id")
    fun deleteCard(id: Long)

    @Update
    fun updateCard(card: Card)


    @Query("SELECT * FROM credit_card_records WHERE cardId=:cardId")
    fun getAllCardRecords(cardId: Long) : LiveData<List<CardRecord>>

    @Query("SELECT * FROM credit_card_records WHERE cardId=:cardId AND id > :from AND id < :to")
    fun getCardRecordsByTime(cardId: Long, from: Long, to: Long): LiveData<List<CardRecord>>

    @Insert
    fun addCardRecord(cardRecord: CardRecord)

    @Query("DELETE FROM credit_card_records WHERE id=:id")
    fun deleteCardRecord(id: Long)

    @Query("DELETE FROM credit_card_records WHERE cardId=:id")
    fun deleteCardRecordByCard(id: Long)
}